﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Antioch.Base
{
    internal class PlayerInputMode : InputMode
    {
        PlayerControllerBase Controller = null;

        public PlayerInputMode(PlayerControllerBase Controller)
        {
            this.Controller = Controller;
        }

        public void Update(EventBasedInput Input, Jemgine.Engine.Simulation Sim)
        {
            bool JumpPressed = Input.QueryButton(EventBasedInput.Buttons.A, 0).LastEvent != EventBasedInput.EventTypes.None;
            bool UsePressed = Input.QueryButton(EventBasedInput.Buttons.Y, 0).LastEvent != EventBasedInput.EventTypes.None;
            bool FirePressed = 
                Input.QueryButton(EventBasedInput.Buttons.DNU_RIGHTTRIGGER, 0).LastEvent != EventBasedInput.EventTypes.None;

            if (UsePressed)
            {
                var UseActionModule = Sim.GetModule<Jemgine.Engine.ActionIconModule>();
                if (UseActionModule != null)
                    UseActionModule.UseActionPressed();
            }

            Vector2 Aim = Input.QueryButton(EventBasedInput.Buttons.DNU_RIGHTSTICK, 0).StickPosition;
            Vector2 Navigation = Input.QueryButton(EventBasedInput.Buttons.DNU_LEFTSTICK, 0).StickPosition;

            var MouseAim = Input.QueryButton(EventBasedInput.Buttons.DNU_MOUSE, 0).StickPosition;
            MouseAim = Sim.CameraModule.Camera.UnProject(MouseAim);
            
            var MouseAimRel = MouseAim - Controller.Me.Position;
            MouseAimRel.Normalize();

            Controller.Update(JumpPressed, UsePressed, FirePressed, Navigation, MouseAimRel, MouseAim);
        }
    }
}
